// /**
//  * StereoHelper
//  */
import tool from '../../../../utils/tool';
import { STEREO_TYPE } from '../../../../utils/consts';

export const StereoHelper = (function (){
    // 组件文字
    let texts = {
        nav: $._('navigation position'),
        link: $._('Web Link'),
        call: $._("Make a call"),
        extend: $._("Introduction to expand"),
        download: $._("APP DownLoad")
    };
    let urls = {
        lookers: "/static/new_version/image/icon_looker_widget.png",
        redbag: "/static/idealsee/images/new_editor/editor/rebag.png",
        threed: "/static/idealsee/images/new_editor/editor/iconfodnt-3d.png",
        hopebank: "/static/idealsee/images/new_editor/editor/hope_redbag.png",
    };
    const CONST = {
        BASE_TYPE_IMG_SIZE: 20,
        EXTRA_TYPE_MARGIN: 10,
        EXTRA_TYPE_IMG_SIZE: 14,
        EXTRA_TYPE_TEXT_WIDTH: 50,
        EXTRA_TYPE_TEXT_HEIGHT: 14,
        EXTRA_TYPE_TEXT_FONT_SIZE: 12,
        THREED_STATE_TEXT_FONT_SIZE: 12,
        THREED_STATE_CONTAINER_WIDTH: 100,
        THREED_STATE_CONTAINER_HEIGHT: 45
    };
    let excludeAttrs = [
        "scaleX",
        "scaleY",
        "imgPosX",
        "imgPosY",
        "imgWidth",
        "imgHeight",
        "textPosX",
        "textPosY",
        "text1PosX",
        "text1PosY",
        "text2PosX",
        "text2PosY",
        "textSize",
        "stateColor",
        "stateWidth",
        "stateHeight",
        "stateStroke",
        "stateBackground",
        "stereoUrl",
        "stereoText",
        "stereoType",
        "stereoColor",
        "stereoRotateX",
        "stereoRotateY",
        "stereoRotateZ",
        "nonSelectable"
    ];
    let stereos = [];
    let getIndexByStereoId = function (id){
        for (let i=0,l=stereos.length; i<l; i++){
            if (id === stereos[i].id){
                return i;
            }
        }
        return -1;
    };
    // 剔除3d场景中添加的各种额外属性
    let removeExcludeAttrs = function (datas){
        let specify = datas || stereos;
        specify.forEach(function (stereo){
            for (let key in stereo){
                if (stereo.hasOwnProperty(key)){
                    if ($.inArray(key, excludeAttrs) != -1){
                        delete stereo[key];
                    }
                }
            }
        });
    };
    let rebuildWidgetData = function (data){
        data.custom = data.custom == undefined ? 0 : data.custom * 1;
        if (data.icon == undefined){
            data.icon = true;
        }
    };
    let rebuildStereoData = function (data){
        let isBright = data.color ? tool.isBrightColor(data.color || "000000") : false;
        let iconSrcm = tool.getSrcmByTypeAndBright(data.type, isBright);

        let type = STEREO_TYPE[data.type];
        data.stereoType = type;
        data.stereoColor = (isBright ? "#000" : "#FFF");
        data.stereoUrl = iconSrcm || urls[type] || data.url;
        data.stereoText = data.text || data.tip_text || texts[type];
        // 角度转弧度
        data.stereoRotateX = -(THREE.Math.degToRad(data.rotate_x));
        data.stereoRotateY = -(THREE.Math.degToRad(data.rotate_y));
        // 翻转旋转角度，修正2d和3d中的区别
        data.stereoRotateZ = -(THREE.Math.degToRad(data.rotate_z));
    };
    let rebuildThreedData = function (data){
        data.stateColor = "#8b95a7";
        data.stateStroke = "#f87929";
        data.stateBackground = "#ffffff";
        data.stateWidth = CONST.THREED_STATE_CONTAINER_WIDTH;
        data.stateHeight = CONST.THREED_STATE_CONTAINER_HEIGHT;
    };
    let rebuildPositionData = function (data){
        let baseTypes = ["threed", "music"];
        let extraTypes = ["link", "call", "download", "nav", "extend", "nearBy", "form"];

        let type = data.stereoType;
        let width = data.width, height = data.height;
        data.textSize = CONST.EXTRA_TYPE_TEXT_FONT_SIZE;
        if ($.inArray(type, extraTypes) != -1){
            data.imgPosX = (width - CONST.EXTRA_TYPE_IMG_SIZE - CONST.EXTRA_TYPE_MARGIN - CONST.EXTRA_TYPE_TEXT_WIDTH) / 2;
            data.imgPosY = (height - CONST.EXTRA_TYPE_IMG_SIZE) / 2;
            data.textPosX = data.imgPosX + CONST.EXTRA_TYPE_IMG_SIZE + CONST.EXTRA_TYPE_MARGIN;
            // 10为偏差量
            data.textPosY = (height - CONST.EXTRA_TYPE_TEXT_HEIGHT) / 2 + 10;
            data.imgWidth = data.imgHeight = CONST.EXTRA_TYPE_IMG_SIZE;
        } else if($.inArray(type, baseTypes) != -1) {
            data.imgWidth = data.imgHeight = CONST.BASE_TYPE_IMG_SIZE;
            data.imgPosX = data.imgPosY = width / 2 - CONST.BASE_TYPE_IMG_SIZE / 2;

            // 计算模型状态相关属性
            let text1Length = 0, text2Length = 0;
            if (data.msg1 != undefined && data.msg2 != undefined){
                // 只有一行文字
                if (data.msg2 === ""){
                    text1Length = data.msg1.replace("...", "x").length;

                    data.text1PosX = data.width + CONST.THREED_STATE_TEXT_FONT_SIZE;
                    data.text1PosY = (CONST.THREED_STATE_CONTAINER_HEIGHT - CONST.THREED_STATE_TEXT_FONT_SIZE) - 5;
                // 两行文字
                } else {
                    text1Length = data.msg1.length;
                    text2Length = data.msg2.length;

                    data.text1PosX = data.width + (CONST.THREED_STATE_CONTAINER_WIDTH - text1Length * CONST.THREED_STATE_TEXT_FONT_SIZE) / 2;
                    data.text1PosY = (CONST.THREED_STATE_CONTAINER_HEIGHT - (CONST.THREED_STATE_TEXT_FONT_SIZE) * 2) - 5;

                    data.text2PosX = data.width + (CONST.THREED_STATE_CONTAINER_WIDTH - text2Length * CONST.THREED_STATE_TEXT_FONT_SIZE) / 2;
                    data.text2PosY = data.text1PosY + (CONST.THREED_STATE_TEXT_FONT_SIZE) + 5;
                }
            }
        }
    };
    let translateData = function (item){
        item.data.offset_x = parseInt(item.position.x);
        item.data.offset_y = parseInt(item.position.y);
        item.data.offset_z = parseInt(item.position.z);
        updateStereo(item.data);

        removeExcludeAttrs([item.data]);
        // stereo.storageData(item.data);
    };
    let rotateData = function (item){
        item.rotation.x = item.rotation.x === undefined ? 0 : item.rotation.x;
        item.data.rotate_x = -(THREE.Math.radToDeg(item.rotation.x));
        item.data.rotate_y = -(THREE.Math.radToDeg(item.rotation.y));
        item.data.rotate_z = -(THREE.Math.radToDeg(item.rotation.z));
        item.data.stereoRotateX = item.rotation.x;
        item.data.stereoRotateY = item.rotation.y;
        item.data.stereoRotateZ = item.rotation.z;
        updateStereo(item.data);

        removeExcludeAttrs([item.data]);
        // stereo.storageData(item.data);
    };
    let scaleData = function (item){
        let scaleX = item.scale.x < 0 ? 1 : item.scale.x;
        let scaleY = item.scale.y < 0 ? 1 : item.scale.y;
        item.data.scaleX = item.scale.x;
        item.data.scaleY = item.scale.y;
        item.data.width = (scaleX * item.data.width) * 1;
        item.data.height = (scaleY * item.data.height) * 1;
        updateStereo(item.data);

        removeExcludeAttrs([item.data]);
        // stereo.storageData(item.data);
    };
    let removeStereoById = function (id){
        let index = getIndexByStereoId(id);
        if (index !== -1){
            stereos.splice(index, 1);
        }
    };
    let removeStereosByIds = function (ids){
        ids.forEach(function (id){
            removeStereoById(id);
        });
    };
    let addStereos = function (data){
        data.forEach(function (stereo){
            let index = getIndexByStereoId(stereo.id);
            if (index === -1){
                stereos.push(stereo);
            }
        });
    };
    let updateStereo = function (stereo){
        let index = getIndexByStereoId(stereo.id);
        if (index !== -1){
            stereos.splice(index, 1, stereo);
        }
    };
    let clearStereos = function (){
        stereos = [];
    };
    let getStereos = function (){
        removeExcludeAttrs();
        return stereos;
    };
    return {
        scaleData: scaleData,
        rotateData: rotateData,
        addStereos: addStereos,
        getStereos: getStereos,
        updateStereo: updateStereo,
        clearStereos: clearStereos,
        translateData: translateData,
        rebuildWidgetData: rebuildWidgetData,
        rebuildStereoData: rebuildStereoData,
        rebuildThreedData: rebuildThreedData,
        removeStereosByIds: removeStereosByIds,
        rebuildPositionData: rebuildPositionData
    }
})();


export const MeshHelper = (function (){
    let meshes = [];

    let getIndexById = function (id){
        for (let i=0,l=meshes.length; i<l; i++){
            if (id === meshes[i].data.id){
                return i;
            }
        }
        return -1;
    };

    let removeMeshById = function (id){
        let index = getIndexById(id);
        if (index !== -1){
            meshes.splice(index, 1);
        }
    };

    let getMeshById = function (id){
        let results = meshes.filter(function (mesh){
            return mesh.data.id === id;
        });
        return results[0];
    };

    let addMesh = function (mesh){
        let index = getIndexById(mesh.data.id);
        if (index === -1){
            meshes.push(mesh);
        }
    };

    let getMeshes = function (){
        return meshes;
    };

    let clearMeshes = function (fn){
        while (meshes.length){
            let mesh = meshes.pop();
            fn && fn(mesh);
        }
    };

    return {
        addMesh: addMesh,
        getMeshes: getMeshes,
        getMeshById: getMeshById,
        clearMeshes: clearMeshes,
        removeMeshById: removeMeshById
    }
})();


export const TextHelper = (function() {
  const CONST = {
    RATIO: {
      PT2PX: {
        FONT_SIZE: 0.75,
        LINE_HEIGHT: 0.525
      }
    }
  };
  // 根据传入的pt单位换算成px单位的size和lineheight
  let getFontSizeAndLineHeightByPt = function(pt) {
    let fontSize = (pt / CONST.RATIO.PT2PX.FONT_SIZE).toFixed(4) * 1;
    let lineHeight = (pt / CONST.RATIO.PT2PX.LINE_HEIGHT).toFixed(4) * 1;

    return {
      fontSize: fontSize,
      lineHeight: lineHeight
    }
  };

  return {
    getFontSizeAndLineHeightByPt: getFontSizeAndLineHeightByPt
  }
})();

